package com.LeeCode;

/**
 * 大小为 K 且平均值大于等于阈值的子数组数目
 */

public class Code1343 {
    public static void main(String[] args) {
        int[] arr = {2, 2, 2, 2, 5, 5, 5, 8};
        int k = 3, threshold = 4;
        System.out.println(new Code1343().numOfSubarrays(arr, k, threshold));
    }

    public int numOfSubarrays(int[] arr, int k, int threshold) {
        int sum = 0, count = 0;

        for (int i = 0; i < k; i++) {
            sum += arr[i];
        }
        if (sum >= threshold * k) {
            count++;
        }

        for (int i = k; i < arr.length; i++) {
            sum += arr[i] - arr[i - k];
            if (sum >= threshold * k) {
                count++;
            }
        }
        return count;
    }
}
